Skip to content

[OpenVINO backend] Support numpy.prod #21567

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

samthakur587
Copy link
Contributor

@samthakur587 samthakur587 commented Aug 9, 2025

in this pr i have added support of numpy.prod to openvino backend

@gbaned could you please review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary of Changes

Hello @samthakur587, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces support for the "numpy.prod" operation within the Keras OpenVINO backend. Previously, this operation was not implemented, and tests related to it were excluded. With this change, "numpy.prod" can now be used with the OpenVINO backend, expanding its numerical computation capabilities.

Highlights

  • Enable "numpy.prod" support: The core change is the implementation of the "prod" function in "keras/src/backend/openvino/numpy.py", allowing the OpenVINO backend to correctly handle product reductions.
  • Remove "prod" from test exclusions: Correspondingly, tests for "numpy.prod" (specifically "NumpyDtypeTest::test_prod" and "NumpyOneInputOpsCorrectnessTest::test_prod") have been removed from the "excluded_concrete_tests.txt" file, indicating they are now expected to pass.
  • Robust "prod" implementation: The new "prod" implementation handles various scenarios, including type conversions (e.g., boolean to integer for reduction), processing "axis=None" by flattening the input, and managing "dtype" promotion and conversion to OpenVINO types.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments or fill out our survey to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds support for numpy.prod to the OpenVINO backend. The changes look good, but I found a few issues in the implementation of the prod function related to dtype handling. Specifically, there's an undefined function call that would lead to a runtime error, and incorrect dtype promotion for uint64 inputs. I've provided a single comment with a suggested refactoring that addresses these issues and improves the overall code quality.

Comment on lines 1306 to 1341
x = get_ov_output(x)
x_type = x.get_element_type()

# Convert to appropriate type for reduction
if x_type == Type.boolean:
# Convert boolean to int32 for reduction
x = ov_opset.convert(x, Type.i32).output(0)

if axis is None:
flatten_shape = ov_opset.constant([-1], Type.i32).output(0)
x = ov_opset.reshape(x, flatten_shape, False).output(0)
axis = 0

if isinstance(axis, tuple):
axis = list(axis)
axis = ov_opset.constant(axis, Type.i32).output(0)

# Compute the product
result = ov_opset.reduce_prod(x, axis, keepdims).output(0)

# Convert to the specified dtype if provided
if dtype is not None:
ov_dtype = _convert_to_ov_type(dtype)
result = ov_opset.convert(result, ov_dtype).output(0)
else:
# Handle dtype promotion rules
if x_type == Type.boolean:
result = ov_opset.convert(result, Type.i32).output(0)
elif x_type == Type.i64:
result = ov_opset.convert(result, Type.i64).output(0)
elif x_type in (Type.u8, Type.u16, Type.u32):
result = ov_opset.convert(result, Type.u32).output(0)
elif x_type.is_integral():
result = ov_opset.convert(result, Type.i32).output(0)

return OpenVINOKerasTensor(result)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The current implementation has a couple of issues:

  1. _convert_to_ov_type is not defined, which will cause a NameError at runtime.
  2. The dtype promotion logic for uint64 is incorrect, as it gets converted to int32, which can lead to data loss.
  3. There's a redundant type conversion for boolean inputs, which is inefficient.

I suggest refactoring the function to handle dtype promotion before the reduction. This makes the logic cleaner, more efficient, and corrects the bugs.

    x = get_ov_output(x)

    # If a specific dtype is requested, cast the input to that dtype.
    if dtype is not None:
        ov_dtype = OPENVINO_DTYPES[standardize_dtype(dtype)]
        x = ov_opset.convert(x, ov_dtype).output(0)
    # Otherwise, apply dtype promotion rules before reduction.
    else:
        x_type = x.get_element_type()
        if x_type == Type.boolean:
            x = ov_opset.convert(x, Type.i32).output(0)
        elif x_type in (Type.i8, Type.i16):
            x = ov_opset.convert(x, Type.i32).output(0)
        elif x_type in (Type.u8, Type.u16):
            x = ov_opset.convert(x, Type.u32).output(0)

    if axis is None:
        flatten_shape = ov_opset.constant([-1], Type.i32).output(0)
        x = ov_opset.reshape(x, flatten_shape, False).output(0)
        axis = 0

    if isinstance(axis, tuple):
        axis = list(axis)
    axis = ov_opset.constant(axis, Type.i32).output(0)

    # Compute the product
    result = ov_opset.reduce_prod(x, axis, keepdims).output(0)

    return OpenVINOKerasTensor(result)

@codecov-commenter
Copy link

codecov-commenter commented Aug 9, 2025

Codecov Report

❌ Patch coverage is 85.00000% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.75%. Comparing base (ea62750) to head (a173928).

Files with missing lines Patch % Lines
keras/src/backend/openvino/numpy.py 85.00% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master   #21567   +/-   ##
=======================================
  Coverage   82.75%   82.75%           
=======================================
  Files         567      567           
  Lines       56468    56488   +20     
  Branches     8818     8824    +6     
=======================================
+ Hits        46730    46747   +17     
- Misses       7577     7579    +2     
- Partials     2161     2162    +1     
Flag Coverage Δ
keras 82.56% <85.00%> (+<0.01%) ⬆️
keras-jax 63.76% <0.00%> (-0.03%) ⬇️
keras-numpy 58.27% <0.00%> (-0.03%) ⬇️
keras-openvino 34.71% <85.00%> (+0.03%) ⬆️
keras-tensorflow 64.19% <0.00%> (-0.03%) ⬇️
keras-torch 63.81% <0.00%> (-0.03%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@hertschuh
Copy link
Collaborator

@gbaned gbaned requested a review from hertschuh August 18, 2025 07:24
@gbaned gbaned added this to PR Queue Aug 18, 2025
@github-project-automation github-project-automation bot moved this to Assigned Reviewer in PR Queue Aug 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Assigned Reviewer
Development

Successfully merging this pull request may close these issues.

4 participants